/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameCalculoTorisferico.java
 *
 * Created on Jan 5, 2012, 5:05:28 PM
 */
package ar.com.wholeconsulting.pacc.gui.modelos.calculos;

import ar.com.wholeconsulting.pacc.gui.utils.table.TableShowFieldsModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericEditorItems;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericTreeTableModel;
import ar.com.wholeconsulting.pacc.model.Acero;
import ar.com.wholeconsulting.pacc.model.MetodoSoldadura;
import ar.com.wholeconsulting.pacc.model.Modelo;
import ar.com.wholeconsulting.pacc.model.RadiografiaSoldadura;
import ar.com.wholeconsulting.pacc.model.Soldadura;
import ar.com.wholeconsulting.pacc.model.cabezal.CabezalTorisferico;
import ar.com.wholeconsulting.pacc.persistance.JPASession;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

/**
 *
 * @author pable
 */
public class FrameCalculoTorisferico extends javax.swing.JDialog {

    private static final String VALOR_COLUMN = "Valor";
    private CabezalTorisferico currentCabezal;
    private Modelo currentModelo;
    private boolean ok = false;
    private boolean isNew = false;

    /**
     * Creates new form FrameCalculoTorisferico
     */
    public FrameCalculoTorisferico(JDialog frame,Modelo modelo) {
        super(frame, true);
        currentModelo = modelo;
        isNew = true;
        currentCabezal = new CabezalTorisferico();
        modelo.getElementos().add(currentCabezal);
        this.initComponents();
        initComponentsView();
    }

    public FrameCalculoTorisferico(JDialog frame,Modelo modelo, CabezalTorisferico cabezal) {
        super(frame, true);
        this.currentCabezal = cabezal;
        currentModelo = modelo;
        this.initComponents();
        initComponentsView();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        textFabricante = new javax.swing.JTextField();
        textModelo = new javax.swing.JTextField();
        jSpinner1 = new javax.swing.JSpinner();
        buttonCancel = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        panelAtributos = new javax.swing.JScrollPane();
        treeTableAtributos = new org.jdesktop.swingx.JXTreeTable();
        panelCalculos = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        textEspesorNominal = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableValoresCalculados = new javax.swing.JTable();

        setTitle("Cabezales Torisfericos");
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Modelo"));

        jLabel2.setText("Fabricante:");

        jLabel3.setText("Modelo:");

        jLabel8.setText("Cantidad:");

        textFabricante.setEditable(false);
        textFabricante.setEnabled(false);

        textModelo.setEditable(false);
        textModelo.setEnabled(false);

        jSpinner1.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(textFabricante)
                    .addComponent(textModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSpinner1, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textFabricante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel3)
                    .addComponent(textModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/trash_16x16.gif"))); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/save_16x16.gif"))); // NOI18N
        buttonSave.setMaximumSize(new java.awt.Dimension(68, 29));
        buttonSave.setMinimumSize(new java.awt.Dimension(68, 29));
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        treeTableAtributos.setClosedIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/box_closed.png")));
        treeTableAtributos.setLeafIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/page_white_edit.png")));
        treeTableAtributos.setOpenIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/box_open.png")));
        panelAtributos.setViewportView(treeTableAtributos);

        jTabbedPane1.addTab("Atributos", panelAtributos);

        jLabel22.setText("Espesor Nominal:");
        panelCalculos.add(jLabel22);

        textEspesorNominal.setText("0");
        panelCalculos.add(textEspesorNominal);

        jScrollPane1.setViewportView(tableValoresCalculados);

        panelCalculos.add(jScrollPane1);

        jTabbedPane1.addTab("Valores Calculados", panelCalculos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 475, Short.MAX_VALUE)
                        .addComponent(buttonSave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonCancel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonSave, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-563)/2, (screenSize.height-524)/2, 563, 524);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        currentCabezal.setEspesorNominal(Double.parseDouble(textEspesorNominal.getText()));
        ok = true;
        this.setVisible(false);
    }//GEN-LAST:event_buttonSaveActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        ok = false;
        if (!isNew) {
            JPASession.getInstance().getManager().refresh(currentCabezal);
        }
        this.setVisible(false);
    }//GEN-LAST:event_buttonCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonSave;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JScrollPane panelAtributos;
    private javax.swing.JPanel panelCalculos;
    private javax.swing.JTable tableValoresCalculados;
    private javax.swing.JTextField textEspesorNominal;
    private javax.swing.JTextField textFabricante;
    private javax.swing.JTextField textModelo;
    private org.jdesktop.swingx.JXTreeTable treeTableAtributos;
    // End of variables declaration//GEN-END:variables

    private void initComponentsView() {
        textFabricante.setText(currentModelo.getFabricante().getNombre());
        textModelo.setText(currentModelo.getNombre());
        this.setTitle(currentModelo.getFabricante() + " - " + currentModelo.getNombre());
        textEspesorNominal.setText(String.valueOf(currentCabezal.getEspesorNominal()));
        initTreeAtributos();
        initTableCalculos();
    }

    private void initTreeAtributos() {
        GenericTreeTableModel model = new GenericTreeTableModel(treeTableAtributos);
        List<GenericEditorItems> list = new LinkedList<GenericEditorItems>();
        list.add(new GenericEditorItems("Dimensiones",
                new String[]{"Diametro", "Pollera", "Radio Externo", "Radio Interno 1", "Radio Interno 2"}, new String[]{"diametro", "pollera", "l1", "r1", "r0"},
                new DefaultCellEditor[]{null, null, null, null, null}, currentCabezal));

        list.add(new GenericEditorItems("Soldaduras y Eficiencia de Juntas",
                new String[]{"Soldadura del Disco", "Radio del Disco", "Eficiencia de Junta", "Tipo de Soldadura Circulo", "Metodo de Soldadura Circulo", "Radio de Soldadura Circulo"},
                new String[]{"SoldaduraInterna", "RadiografiaInterna", "EficienciaDeJunta", "TipoSoldadura", "MetodoSoldaduraExterna", "RadiografiaExterna"},
                new DefaultCellEditor[]{new DefaultCellEditor(new JComboBox(Soldadura.values())), new DefaultCellEditor(new JComboBox(RadiografiaSoldadura.values())), null,
                    new DefaultCellEditor(new JComboBox(Soldadura.values())),
                    new DefaultCellEditor(new JComboBox(MetodoSoldadura.values())),
                    new DefaultCellEditor(new JComboBox(RadiografiaSoldadura.values()))}, currentCabezal));
        list.add(new GenericEditorItems("Consideracion del Material",
                new String[]{"Tension Admisible", "Material"},
                new String[]{"tensionAdmisible", "acero"},
                new DefaultCellEditor[]{null,
                    new DefaultCellEditor(new JComboBox(JPASession.getInstance().getResultListNamedQuery(Acero.FIND_ALL).toArray()))}, currentCabezal));
        model.initTreeTable(list);
        treeTableAtributos.updateUI();
    }

    private void initTableCalculos() {
        TableModel model = new TableShowFieldsModel(new Object [][] {
                {"Espesor Minimo Requerido", null},
                {"Desarrollo", null},
                {"Capacidad", null},
                {"Peso(kg)", null}
            }, tableValoresCalculados, new TableCellEditor[5],
                new String[]{"espesorDeCalculo","desarrollo","volumen","peso"}, currentCabezal);
        tableValoresCalculados.setModel(model);
        tableValoresCalculados.updateUI();
    }

    public boolean isOk() {
        return ok;
    }
}
